import { Route, Redirect, RouteProps } from 'react-router-dom';
import { isHaveToken } from '@/utils/storage';

interface AuthRoutePropsType extends RouteProps {
  component: React.FC<any>;
  path: string;
}

export type LoactionStateType = {
  from: string;
};

export default function AuthRoute(props: AuthRoutePropsType) {
  const { component: Components, path, ...restProps } = props;
  return (
    <Route
      {...restProps}
      path={path}
      render={(_props) => {
        if (isHaveToken()) return <Components {..._props}></Components>;
        return (
          <Redirect
            to={{
              pathname: '/login',
              state: {
                from: path,
              } as LoactionStateType,
            }}
          />
        );
      }}
    />
  );
}
